package com.webapp.cognos;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import com.cognos.developer.schemas.bibus._3.BaseClass;
import com.cognos.developer.schemas.bibus._3.ContentManagerServiceStub;
import com.cognos.developer.schemas.bibus._3.Group;
import com.cognos.developer.schemas.bibus._3.Policy;
import com.cognos.developer.schemas.bibus._3.PropEnum;
import com.cognos.developer.schemas.bibus._3.QueryOptions;
import com.cognos.developer.schemas.bibus._3.Role;
import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
import com.cognos.developer.schemas.bibus._3.Sort;
import com.inforim.cognos.CognosClient;
import com.webapp.module.CogUser;
import com.webapp.util.BaseLog;

public class CogOpt extends BaseLog
{
  List<CogUser> userlist = new ArrayList();

  public void getRolePerFromCogObj(CognosClient client, String obj_searchpath)
  {
    BaseClass[] results = new BaseClass[0];
    ContentManagerServiceStub cmServiceStub = (ContentManagerServiceStub)client.getService("ContentManagerService");
    try {
      results = cmServiceStub.query(
        new SearchPathMultipleObject(obj_searchpath), 
        new PropEnum[] { PropEnum.searchPath, PropEnum.policies }, 
        new Sort[0], new QueryOptions());
    } catch (RemoteException remoteEx) {
      System.out.println("Caught Remote Exception:\n");
      remoteEx.printStackTrace();
    }

    for (int i = 0; i < results[0].getPolicies().getValue().length; ++i) {
      Policy pol = results[0].getPolicies().getValue()[i];
      getUserFromRole(cmServiceStub, 
        pol.getSecurityObject().getSearchPath().getValue());
    }
  }

  public void getUserFromRole(ContentManagerServiceStub cmServiceStub, String rolesearchpath) {
    PropEnum[] props = { PropEnum.searchPath, PropEnum.defaultName, 
      PropEnum.members };
    if (cmServiceStub != null) {
      Sort[] sortArray = { new Sort() };
      try {
        BaseClass[] bc = cmServiceStub.query(
          new SearchPathMultipleObject(rolesearchpath), props, 
          sortArray, new QueryOptions());
        if (bc == null)
          logger.info("\n\nError occurred in function viewAll.");
        if (bc.length <= 0) {
          logger.info("There are currently no user.");
        }
//        for (int i = 0; i < bc.length; ++i) {
//          String objectName = bc[i].getDefaultName().getValue();
//          String objectPath = bc[i].getSearchPath().getValue();

//          BaseClass[] bcchildren = new BaseClass[1];

//          if (bc[i] instanceof Role) {
//	            bcchildren = ((Role)bc[i]).getMembers().getValue();
//	          }else if(bc[i] instanceof Group){
//	        	  bcchildren=((Group)bc[i]).getMembers().getValue();
//	          }
//	          else {
//	            bcchildren[0] = bc[i];
//	          }
          getAccount(bc, cmServiceStub);
//	          for (int j = 0; (bcchildren != null) && (j < bcchildren.length); ++j) {
//	            BaseClass[] bcuser = cmServiceStub.query(
//	              new SearchPathMultipleObject(
//	              bcchildren[j].getSearchPath().getValue()), props, 
//	              sortArray, new QueryOptions());
//	            String mudid=bcuser[0].getSearchPath().getValue();
//	            int uid=mudid.indexOf("uid=");
//	            boolean has_mudid=false;
//	            if(mudid!=null&&uid>=0){
//	            	mudid=mudid.substring(uid);
//		            int index1=mudid.indexOf(",");
//		            int index2=mudid.indexOf(")");
//		            mudid=mudid.substring("uid=".length(),Math.min(index1, index2));
//		            has_mudid=true;
//	            }
//	            String emailName=bcuser[0].getDefaultName().getValue();
//	            if(has_mudid){
//	            	emailName=mudid;
//	            }
//	            CogUser cu = new CogUser();
//	            cu.setCoguser_name(
//	              bcuser[0].getDefaultName().getValue());
//	            cu.setCoguser_searchpath(
//	              bcuser[0].getSearchPath().getValue());
//	            cu.setCoguser_email(
//	            		emailName + 
//	              "@gsk.com");
//	            this.userlist.add(cu);
//	          }
//        }
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
private void getAccount(BaseClass[] bc,ContentManagerServiceStub cmServiceStub) throws RemoteException{
	Sort[] sortArray = { new Sort() };
	PropEnum[] props = { PropEnum.searchPath, PropEnum.defaultName, 
		      PropEnum.members };
  
	BaseClass[] bcchildren = new BaseClass[1];
	for (int i = 0; (bc != null) && (i < bc.length); ++i) {
		if (bc[i] instanceof Role) {
		    bcchildren = ((Role)bc[i]).getMembers().getValue();
		    getAccount(bcchildren, cmServiceStub);
		  }else if(bc[i] instanceof Group){
			  if(((Group)bc[i]).getMembers()!=null){
				  bcchildren=((Group)bc[i]).getMembers().getValue();
			  }else{
				  bcchildren=cmServiceStub.query(
				          new SearchPathMultipleObject(
				        		  bc[i].getSearchPath().getValue()), props, 
				          sortArray, new QueryOptions());
			  }
			  
			  
			  getAccount(bcchildren, cmServiceStub);
		  }
		  else {
		    bcchildren[0] = bc[i];
		  }
        BaseClass[] bcuser = cmServiceStub.query(
          new SearchPathMultipleObject(
        		  bc[i].getSearchPath().getValue()), props, 
          sortArray, new QueryOptions());
        
        
        
        String mudid=bcuser[0].getSearchPath().getValue();
        int uid=mudid.indexOf("uid=");
        boolean has_mudid=false;
        if(mudid!=null&&uid>=0){
        	mudid=mudid.substring(uid);
            int index1=mudid.indexOf(",");
            int index2=mudid.indexOf(")");
            mudid=mudid.substring("uid=".length(),Math.min(index1, index2));
            has_mudid=true;
        }
        String emailName=bcuser[0].getDefaultName().getValue();
        if(has_mudid){
        	emailName=mudid;
        	CogUser cu = new CogUser();
            cu.setCoguser_name(
              bcuser[0].getDefaultName().getValue());
            cu.setCoguser_searchpath(
              bcuser[0].getSearchPath().getValue());
            cu.setCoguser_email(
            		emailName + 
              "@gsk.com");
            this.userlist.add(cu);
        }
        
      }
}
  public List<CogUser> getUserlist() {
    return this.userlist;
  }

  public void setUserlist(List<CogUser> userlist) {
    this.userlist = userlist;
  }


}